package jgl.vajra.management.dialogFragment;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;

import androidx.fragment.app.DialogFragment;

import java.util.HashMap;
import java.util.Map;

import jgl.vajra.management.R;
import jgl.vajra.management.bean.OrderPaymentDetailsBean;
import jgl.vajra.management.databinding.DialogFragmentDelayDetailsBinding;
import jgl.vajra.management.listener.DelayDetailsListener;
import jgl.vajra.management.manager.AccountManager;
import jgl.vajra.management.widget.dialogfragment.BaseDialogFragment;
import jgl.vajra.management.widget.manager.RxJavaHttpManager;
import jgl.vajra.management.widget.manager.retrofit.RetrofitJsonManager;
import jgl.vajra.management.widget.retrofithelper.rxsubscriber.ProgressObserver;
import jgl.vajra.management.widget.utils.ToastUtils;
import jgl.vajra.management.widget.utils.UIUtils;

public class DelayDetailsDialogFragment extends BaseDialogFragment<DialogFragmentDelayDetailsBinding> {

    private DelayDetailsListener listener;
    private OrderPaymentDetailsBean orderPaymentDetailsBean;

    @Override
    public void initView(Context context, View rootView, AttributeSet attrs) {
        mBinding.setView(this);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.BottomSheetEdit);

        mBinding.tvTitle.setText(orderPaymentDetailsBean.clientNickname+"发起延时单子");

        String text = orderPaymentDetailsBean.clientNickname+"延时了"+orderPaymentDetailsBean.overtimeHours+"小时，"+orderPaymentDetailsBean.clientNickname+"将额外支付延时单子金额"+orderPaymentDetailsBean.overtimeAmount+"元。";
        String text1 = orderPaymentDetailsBean.overtimeHours+"小时";
        int i = text.indexOf(text1);
        SpannableStringBuilder builder = new SpannableStringBuilder(text.toString());

        String text2 = orderPaymentDetailsBean.overtimeAmount+"元";
        int i2 = text.indexOf(text2);

        mBinding.tvContent.setMovementMethod(LinkMovementMethod.getInstance());
        ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(UIUtils.getColor(R.color.color_ff0000));
        builder.setSpan(foregroundColorSpan, i, i + text1.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

        ForegroundColorSpan foregroundColorSpan2 = new ForegroundColorSpan(UIUtils.getColor(R.color.color_ff0000));
        builder.setSpan(foregroundColorSpan2, i2, i2 + text2.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        mBinding.tvContent.setText(builder);
        mBinding.tvContent.setHighlightColor(UIUtils.getColor(R.color.transparent));
    }

    @Override
    public void initData(Context context) {
    }

    @Override
    public void initEvent(Context context) {

    }

    public int getLayoutRes() {
        return R.layout.dialog_fragment_delay_details;
    }


    @Override
    public int getGravity() {
        return Gravity.CENTER;
    }


    //确认
    public void onPositive(int tag) {//1-确认客户延时用车，2-拒绝客户延时用车
        Map<String, Object> map = new HashMap<>();
        map.put("Authorization", AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization);
        map.put("id",orderPaymentDetailsBean. overtimeId);//延时用车id号
        map.put("tag",tag);
        RetrofitJsonManager.getInstance().getApiService().determineDelayData(map)
                .compose(RxJavaHttpManager.applyTransformer())
                .subscribe(new ProgressObserver<String>(false, null) {
                    @Override
                    public void _onNext(String data) {
                        ToastUtils.showShort(data);
                        listener.onDetermine();
                        dismiss();
                    }

                    @Override
                    public void _onError(String errorMessage, int errorCode) {
                        super._onError(errorMessage, errorCode);

                    }
                });
    }

    public void setDelayDetailsListener(DelayDetailsListener listener){
        this.listener =listener;
    }

    public void setData(OrderPaymentDetailsBean orderPaymentDetailsBean){
        this.orderPaymentDetailsBean = orderPaymentDetailsBean;
    }
}

